//
//  NetManager.m
//  简单记事本
//
//  Created by 胡先剑 on 16/4/2.
//  Copyright © 2016年 胡先剑. All rights reserved.
//

#import "NetManager.h"

@implementation NetManager

/**
 *  查询记事本
 *
 *  @param block 查询成功时执行的代码
 */
+ (void)searchWithBlock:(void (^) (NSArray *arr))block
{
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com/service/mynotes/WebService.php?email=292710547@qq.com&type=JSON&action=query"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         if (connectionError == nil)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
             block(dic[@"Record"]);
         }
         else
         {
             NSLog(@"%@",connectionError);
         }
     }];
}

/**
 *  添加记事本
 *
 *  @param content 记事本内容
 *  @param date    添加的日期
 *  @param block   添加成功执行的代码
 */
+ (void)addWithContent:(NSString *)content andDate:(NSString *)date andBlock:(void (^) ())block
{
    NSString *urlStr = [NSString stringWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=292710547@qq.com&type=JSON&action=add&content=%@&date=%@",content,date];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
    {
        if (connectionError == nil)
        {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            if ([dic[@"ResultCode"] isEqualToNumber:@1])
            {
                block();
            }
            else
            {
                NSLog(@"添加失败");
            }
        }
        else
        {
            NSLog(@"%@",connectionError);
        }
    }];
}

/**
 *  删除记事本
 *
 *  @param noteId 删除的ID
 *  @param block  删除成功执行的代码
 */
+ (void)removeWithNoteId:(NSNumber *)noteId  andBlock:(void (^) ())block
{
    NSString *urlStr = [NSString stringWithFormat:@"http://www.51work6.com/service/mynotes/WebService.php?email=292710547@qq.com&type=JSON&action=remove&id=%@",noteId];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
         if (connectionError == nil)
         {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
             
             if ([dic[@"ResultCode"] isEqualToNumber:@1])
             {
                 block();
             }
             else
             {
                 NSLog(@"删除失败");
             }
         }
         else
         {
             NSLog(@"%@",connectionError);
         }
     }];
}

@end
